#include "inmersiondelegate.h"

InmersionDelegate::InmersionDelegate( QObject* parent ) : QStyledItemDelegate( parent ) {}

void InmersionDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
    painter->save();
    if( option.state & QStyle::State_Selected )
    {
        painter->fillRect( option.rect, "lightsteelblue" );
        painter->setPen( "steelblue" );
        painter->drawLine( option.rect.topLeft(), option.rect.topRight() );
        painter->drawLine( option.rect.bottomLeft(), option.rect.bottomRight() );
    }
    else if( option.state & QStyle::State_MouseOver )
    {
        painter->fillRect( option.rect, "#eeeeee" );
        painter->setPen( "lightgrey" );
        painter->drawLine( option.rect.topLeft(), option.rect.topRight() );
        painter->drawLine( option.rect.bottomLeft(), option.rect.bottomRight() );
    }
    painter->restore();

    Inmersion* inmersion = index.data( InmersionesSourceModel::InmersionRole ).value<Inmersion*>();
    quint32 margin = 5;
    quint32 numerowidth = 45;
    quint32 separation = 3;

    QFont font;
    font.setPointSize( 20 );
    painter->setFont( font );

    QRect numerorect;
    painter->drawText( option.rect.marginsRemoved( QMargins( margin, margin, margin, margin ) ),
                       Qt::AlignLeft | Qt::AlignTop,
                       QString( "%1" ).arg( inmersion->numero() ),
                       &numerorect );

    font.setPointSize( 11 );
    painter->setFont( font );

    QRect sitiorect;
    int sitiowidth = option.rect.width() - numerowidth - (2*margin);
    painter->drawText( QRect( numerowidth,
                              numerorect.top() + separation,
                              sitiowidth,
                              option.rect.height() - sitiorect.height() - separation - (2*margin) ),
                       Qt::AlignLeft | Qt::AlignTop,
                       painter->fontMetrics().elidedText( inmersion->sitio()->nombre(), Qt::ElideRight, sitiowidth ),
                       &sitiorect );

    font.setPointSize( 7 );
    painter->setFont( font );

    QRect ubicacionrect;
    painter->drawText( QRect( sitiorect.left(),
                              sitiorect.bottom() + separation,
                              option.rect.width() - numerowidth - (2*margin),
                              option.rect.height() - sitiorect.height() - separation - (2*margin) ),
                       Qt::AlignLeft | Qt::AlignTop,
                       QString( "%1 (%2)" ).arg( inmersion->sitio()->ubicacion()->nombre() ).arg( inmersion->sitio()->ubicacion()->pais()->nombre() ),
                       &ubicacionrect );

    QRect iniciorect;
    painter->drawText( QRect( ubicacionrect.left(),
                              ubicacionrect.bottom() + separation,
                              option.rect.width() - numerowidth - (2*margin),
                              option.rect.height() - sitiorect.height() - ubicacionrect.height() - separation - (2*margin) ),
                       Qt::AlignLeft | Qt::AlignTop,
                       QString( "%1 (%2)" ).arg( inmersion->inicio().toString( "ddd dd-MM-yyyy HH:mm:ss" ) ).arg( Utils::formatMs( inmersion->duracion() ) ),
                       &iniciorect );
}
